package com.edushare.activity;

import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.edushare.R;
import com.edushare.controller.GroupController;
import com.edushare.controller.HttpUploader;
import com.edushare.controller.UserFunctions;
import com.edushare.entity.Globals;

public class GroupCreateActivity extends Activity {

	private ImageView groupIcon;
	private EditText groupNameEt, groupDescEt, groupPwdEt;
	private RadioButton privateGroupType;
	private Button createGroupBtn;
	private GroupController gc = new GroupController();
	String groupName, groupDesc, groupPwd;
	Uri currImageURI;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group_create);

		// toggle password field
		groupPwdEt = (EditText) findViewById(R.id.groupPwd);
		privateGroupType = (RadioButton) findViewById(R.id.privateGroupType);
		privateGroupType
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean isChecked) {
						if (isChecked) {
							groupPwdEt.setVisibility(View.VISIBLE);
						} else {
							groupPwdEt.setVisibility(View.INVISIBLE);
						}
					}
				});

		// change group icon
		groupIcon = (ImageView) findViewById(R.id.groupIcon);
		groupIcon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				loadPicture();
			}
		});

		// create button is clicked
		createGroupBtn = (Button) findViewById(R.id.createGroupBtn);
		createGroupBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// get details from view
				groupNameEt = (EditText) findViewById(R.id.groupName);
				groupDescEt = (EditText) findViewById(R.id.groupDesc);
				groupName = groupNameEt.getText().toString();
				groupDesc = groupDescEt.getText().toString();
				groupPwd = groupPwdEt.getText().toString();

				// upload icon and create group
				HttpUploader uploader = new HttpUploader();
				try {
					String imagename = uploader.execute(
							getRealPathFromURI(currImageURI)).get();
					UserFunctions userFn = new UserFunctions();
					Globals.uid = userFn.getUid(getApplicationContext());
					HashMap<String, String> userDetails = userFn
							.getUser(getApplicationContext());
					String userID = userDetails.get("id");
					gc.createGroup(groupName, groupDesc, imagename, groupPwd,
							userID);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}

			}
		});

	}

	/**
	 * Starts an activity for loading a picture.
	 */
	private void loadPicture() {
		Intent intent = new Intent(Intent.ACTION_PICK,
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(final int requestCode,
			final int resultCode, final Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case 1:
				currImageURI = data.getData();
				String[] filePathColumn = { MediaColumns.DATA };

				Cursor cursor = getContentResolver().query(currImageURI,
						filePathColumn, null, null, null);
				cursor.moveToFirst();

				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				String picturePath = cursor.getString(columnIndex);
				cursor.close();

				groupIcon.setImageBitmap(BitmapFactory.decodeFile(picturePath));
				break;
			default:
				break;
			}
		}
	}

	private String getRealPathFromURI(Uri contentUri) {

		String[] proj = { MediaColumns.DATA };
		android.database.Cursor cursor = getContentResolver().query(contentUri,
				proj, // Which
				// columns
				// to
				// return
				null, // WHERE clause; which rows to return (all rows)
				null, // WHERE clause selection arguments (none)
				null); // Order-by clause (ascending by name)
		int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.group_create, menu);
		return true;
	}

}
